
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.OrderGoodsDao;
import com.sun.showcase.biz.mall.service.OrderGoodsService;
import com.sun.showcase.client.domain.mall.OrderGoods;
import com.sun.showcase.client.query.mall.OrderGoodsQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("orderGoodsService")
@Transactional
public class OrderGoodsServiceImpl implements OrderGoodsService{
	@Autowired
	private OrderGoodsDao orderGoodsDao;
	
	public void setOrderGoodsDao(OrderGoodsDao dao) {
		this.orderGoodsDao = dao;
	}

	public DataGrid datagrid(OrderGoodsQuery orderGoodsQuery) {
		DataGrid j = new DataGrid();
		Pager<OrderGoods> pager  = find(orderGoodsQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<OrderGoodsQuery> getQuerysFromEntitys(List<OrderGoods> orderGoodss) {
		List<OrderGoodsQuery> orderGoodsQuerys = new ArrayList<OrderGoodsQuery>();
		if (orderGoodss != null && orderGoodss.size() > 0) {
			for (OrderGoods tb : orderGoodss) {
				OrderGoodsQuery b = new OrderGoodsQuery();
				BeanUtils.copyProperties(tb, b);
				orderGoodsQuerys.add(b);
			}
		}
		return orderGoodsQuerys;
	}

	private Pager<OrderGoods> find(OrderGoodsQuery orderGoodsQuery) {
		Page<OrderGoods> page = PageHelper.startPage(orderGoodsQuery.getPage().intValue(), orderGoodsQuery.getRows().intValue());
		List<OrderGoods> list = orderGoodsDao.findList(orderGoodsQuery);
		Pager<OrderGoods> pager = new Pager<OrderGoods>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public OrderGoods add(OrderGoodsQuery orderGoodsQuery) {
		OrderGoods t = new OrderGoods();
		BeanUtils.copyProperties(orderGoodsQuery, t);
		t.setIfEvaluate(StringConstant.ZERO);
		t.setReturnNum(0);
	    t.setId(UuidUtil.get32UUID());
		orderGoodsDao.save(t);
		orderGoodsQuery.setId(t.getId());
		return t;
		
	}

	public void update(OrderGoodsQuery orderGoodsQuery) {
		OrderGoods t = orderGoodsDao.getById(orderGoodsQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(orderGoodsQuery, t);
	    	orderGoodsDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(OrderGoodsQuery orderGoodsQuery) {
	    orderGoodsDao.updatePart(orderGoodsQuery);
	}

	public void delete(String[] ids) {
		orderGoodsDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		orderGoodsDao.deletePt(ids);
	}

	public OrderGoods get(OrderGoodsQuery orderGoodsQuery) {
		return orderGoodsDao.getById(orderGoodsQuery.getId());
	}

	public OrderGoods get(String id) {
		return orderGoodsDao.getById(id);
	}

	
	public List<OrderGoodsQuery> listAll(OrderGoodsQuery orderGoodsQuery) {
	    List<OrderGoods> list = orderGoodsDao.findList(orderGoodsQuery);
		List<OrderGoodsQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public void comment(String orderId, String goodsId) {
		OrderGoodsQuery up = new OrderGoodsQuery();
		up.getSearchMap().put("order_id", orderId);
		up.getSearchMap().put("goods_id", goodsId);
		up.setIfEvaluate(StringConstant.ONE);
		this.updatePart(up);
	}

	@Override
	public void updateReturnNum(String id, Integer returnNum) {
		OrderGoodsQuery up = new OrderGoodsQuery();
		up.getSearchMap().put(StringConstant.ID, id);
		up.setReturnNum(returnNum);
		this.updatePart(up);
	}
	
	
}
